home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / MacScheme20 / Contributed / SCOOPS / scoops.doc.part1 < prev    next >
Encoding:
Text File  |  1987-12-14  |  12.9 KB  |  253 lines  |  [TEXT/EDIT]

  1. Material extracted from the TI Scheme Language Reference Manual
  2. with permission of the publisher
  3. Copyright 1985 Texas Instruments Incorporated
  4.  
  5. [If you print this documentation or copy it onto another disk, be sure to
  6. include the above copyright and credit notice.]
  7.  
  8. [The user-contributed implementation of SCOOPS that is distributed with
  9. MacScheme may not support all the features described in this documentation.
  10. We have changed the TI documentation only in places that obviously did not
  11. apply to the MacScheme version.  Our omissions are indicated by ellipses,
  12. and our additions by square brackets -- Semantic Microsystems.]
  13.  
  14. SCOOPS is an object-oriented programming system for TI Scheme based on
  15. first-class environments and on multiple and dynamic inheritance.  Although
  16. it is similar in concept and syntax to the object-oriented paradigms of the
  17. LOOPS (2) and Flavors (25) systems, the implementation of SCOOPS relies 
  18. heavily on the features of the Scheme language....
  19.  
  20. OVERVIEW:  The object-oriented programming world consists of objects, which
  21. represent abstract entities. An object is comprised of variables, which
  22. determine the local state of the object, and methods, which define the
  23. object's behavior.  Higher levels of abstraction are built up through
  24. inheritance; that is, higher level classes of objects may inherit the
  25. properties of other classes.  Large systems can thus be divided naturally 
  26. into coherent parts that can be developed and maintained separately.  
  27. Programmers can also avoid the specification of redundant information.
  28.  
  29. The only way to interact with an object or to influence its state is by
  30. sending a message to it.  However, the message passing style need not be
  31. adhered to internally within an object.  Methods may use normal lexical
  32. scoping and procedure calls to access variables and to invoke other methods 
  33. of the same class.  Method definition is, therefore, both convenient and
  34. efficient.
  35.  
  36. Before you use any of the SCOOPS facilities, be sure to load the SCOOPS
  37. environment by entering the following:
  38.  
  39. (load "scoops.sch")
  40.  
  41. [SCOOPS requires a 1 Mby Macintosh.]
  42.  
  43. DEFINING CLASSES:  A class contains the description of one or more similar
  44. objects; an object is an instance of a class.  The definition of a class
  45. consists of class variables, instance variables, methods, and mixins. Class
  46. variables contain information that is shared by all instances of the class.
  47. Instance variables are local to each instance and contain information 
  48. specific to that instance.  Methods are procedures that determine the 
  49. behavior of instances of the class.  A class may inherit the variables and 
  50. methods of other classes through mixins.  Inheritance has proven to be a 
  51. useful way to organize information in complex systems.
  52.  
  53. The following code is an example of class definition in SCOOPS:
  54.  
  55. (define-class employees
  56.   (classvars (no-of-employees 0))
  57.   (instavars name emp-no manager salary (overtime 0))
  58.   (mixins personal-info education-experience)
  59.   (options (gettable-variables name emp-no no-of-employees)
  60.            settable-variables    
  61.            inittable-variables))
  62.  
  63. The class EMPLOYEES has a class variable, NO-OF-EMPLOYEES, which is
  64. initialized to zero.  It also contains five instance variables, NAME, EMP-NO,
  65. MANAGER, SALARY, and OVERTIME. This class inherits class variables, instance
  66. variables, and methods from its mixins, PERSONAL-INFO and
  67. EDUCATION-EXPERIENCE.
  68.  
  69. Methods are generated automatically for gettable and settable variables. The
  70. inittable variables can be given initial values when an instance of a class
  71. is created.  In the class EMPLOYEES, all the variables are settable and
  72. inittable, while only NAME, EMP-NO, and NO-of-EMPLOYEES are gettable.
  73.  
  74. In object-oriented programming systems, it is traditional to allow class
  75. variables, instance variables, methods, and mixins to be added or deleted 
  76. from the definition of the class at any time.  In the current version of 
  77. SCOOPS in PC Scheme, only methods can be added or deleted.  Methods do not 
  78. have to be recompiled when the class defintion is changed.
  79.  
  80. The special form MAKE-INSTANCE is used to create new objects.  Since 
  81. instances are not created with names, it is necessary to keep references to 
  82. them in variables or other data structures.
  83.  
  84. METHODS:  Methods are defined for a class with DEFINE-METHOD. If a method by 
  85. a given name already exists, the next definition replaces the existing one.
  86.  
  87. The following example illustrates the addition of two methods,
  88. EARNINGS-GREATER-THAN and EARNINGS, to the class EMPLOYEES:
  89.  
  90. (define-method (employees earnings) ()
  91.   (+ salary overtime))
  92.  
  93. (define-method (employees earnings-greater-than) (val)
  94.   (if (>? (earnings) val)
  95.       (writeln name emp-no)
  96.       '()))
  97.  
  98. The method EARNINGS computes an employee's earnings as the sum of OVERTIME 
  99. and SALARY. The method EARNINGS-GREATER-THAN prints the name and employee 
  100. number of the employee if the earnings are greater than a given value.  Note 
  101. that methods can refer to instance variables as if they were lexically 
  102. enclosing variables.
  103.  
  104. An object's methods are invoked by sending a message to the object with the
  105. SEND special form.  However, if a method in a class needs to invoke another
  106. method in the same class, including methods inherited from mixins, it may 
  107. call the other method directly as a procedure.  This call is illustrated in 
  108. the previous example, where the method EARNINGS-GREATER-THAN calls EARNINGS,
  109. another method in the same class, as a procedure.
  110.  
  111. VARIABLES:  An object contains two kinds of variables: class variables and
  112. instance variables.  Class variables contain information that is shared by 
  113. all objects of the same class.  Instance variables are local to each object
  114. and contain the object's local state.
  115.  
  116. Within an object, these variables may be treated as ordinary lexical
  117. variables.  This treatment is illustrated in the example in the previous
  118. chapter, where the method EARNINGS adds the values of the instance variables
  119. SALARY and OVERTIME.
  120.  
  121. If the variables have been defined with the option GETTABLE-VARIABLES, 
  122. methods to access their values are generated automatically.  The names of 
  123. these methods are constructed by prefixing the names of the variables with GET-.  For example, in the class EMPLOYEES in the first example, the 
  124. variables NAME, EMP-NO, and NO-OF-EMPLOYEES are gettable.  The value of a 
  125. gettable variable is obtained by sending a message such as (SEND EMP1 
  126. GET-NAME) to an instance.  Similarly, for settable variables, the names of 
  127. the methods are prefixed with SET-, and the value of a settable variable may 
  128. be modified by sending a message such as (SEND EMP1 SET-SALARY 4000) to an 
  129. instance.
  130.  
  131. The values of gettable class variables also may be accessed or set through 
  132. the class itself without referring to an instance of the class.  The special 
  133. forms GETCV and SETCV get and modify the value of a class variable, 
  134. respectively.
  135.  
  136. ACTIVE VALUES:  Active values [2] are used to trigger procedure invocations
  137. whenever the value of a variable is accessed or updated.  Currently, only
  138. instance variables can have active values.  An instance variable is given an
  139. active value with the special form (ACTIVE initial-value getfn setfn).
  140.  
  141. The keyword ACTIVE informs the system that this is an active value.  The
  142. procedure getfn is invoked whenever the value of the variable is accessed, 
  143. and the procedure setfn is invoked whenever the value of the variable is 
  144. modified.  The procedures getfn and setfn may be external procedures, methods 
  145. in the same class, or #F, if no procedure is to be specified.
  146.  
  147. Variables with active values are both gettable and settable, and access and
  148. update methods are generated automatically for them.
  149.  
  150. To trace references to an instance variable POSITION, you could define
  151. POSITION as follows:
  152.  
  153. (define-class shipn
  154.   (instvars (position (active 0 traceget traceset)) ...)
  155.   ...)
  156.  
  157. Whenever the value POSITION is accessed, the current value is passed to
  158. TRACEGET, and the value returned from TRACEGET is returned as the value of
  159. POSITION.  Whenver a value is assigned to the variable POSITION, TRACESET is
  160. invoked with the new value, and POSITION is set to the value returned from
  161. TRACESET.
  162.  
  163. Active values may be nested to an arbitrary depth by specifying another 
  164. active value as the initial-value of an active value:
  165.  
  166. (active (active initial-value getfn1 setfn1) getfn2 setfn2)
  167.  
  168. Whenever the value of the variable bound to this active value is set, the
  169. setfn procedures are invoked in order from the outermost to the innermost. 
  170. Likewise, whenever the value of the variable is accessed, the getfn 
  171. procedures are invoked from the innermost to the outermost.
  172.  
  173. SCOOPS also provides a way to access or modify active values without invoking
  174. the getfn and setfn procedures.
  175.  
  176. Within an object, the value of a class or instance variable can be accessed 
  177. in two ways---as a lexical variable and with its SET- and GET- methods.  The
  178. simplest way is to access it as a lexical variable, using SET!, for example. 
  179. When an active value is modified in this manner, its setfn procedures are not
  180. invoked.
  181.  
  182. The other way to modify an active value is with a procedure call, such as
  183. (SET-POSITION 10).  This call invokes the setfn procedures for the active
  184. value in the manner described previously.  Similarly, if POSITION is simply
  185. referenced as a lexical variable, the current value is returned, and the 
  186. getfn procedures are not invoked.  However, if the GET-POSITION procedure is 
  187. used to retrieve the value, the getfn procedures in the active value are 
  188. invoked in order.
  189.  
  190. INHERITANCE:  Inheritance is used to build higher levels of abstractions. 
  191. Large systems can be organized through an inheritance structure of classes,
  192. thus permitting a modular design and avoiding the specification of redundant
  193. information.
  194.  
  195. In SCOOPS, an inheritance structure is built by "mixing in" other classes to
  196. form an acyclic directed graph.  Classes that are made to be components of
  197. other classes in this way are called mixins.  A class inherits the class
  198. variables, instance variables, and methods from its component classes.  The
  199. order in which the component classes are combined determines which variables
  200. and  methods are inherited when naming conflicts arise.  The inheritance
  201. graph is searched in depth-first order, omitting any nodes that have been
  202. visited previously.  For example, if CLASS1 has mixins CLASS2 and CLASS3,
  203. CLASS2 has mixins CLASS4 and CLASS5, and CLASS3 has mixins CLASS4 and CLASS6,
  204. the order in which the classes are combined is CLASS1, CLASS2, CLASS4,
  205. CLASS5, CLASS3, CLASS6.
  206.  
  207. The set of class variables and instance variables of a class is the union of
  208. class variables and instance variables of its component classes.  However, 
  209. the class variables that CLASS1 inherits from its component classes are 
  210. shared by all instances of CLASS1 but are not shared by instances of its 
  211. component classes.
  212.  
  213. Methods also are combined in a simple manner. If two classes contain a method
  214. with the same name, the method whose class is closer to the root in the
  215. depth-first order is inherited.  Methods may be added or deleted at any time. 
  216. The changes made to any classes are propagated throughout the inheritance
  217. structure.
  218.  
  219. The actual inheritance structure for a class is not contructed until the 
  220. class is compiled with the COMPILE-CLASS operation.  This operation is 
  221. performed automatically the first time MAKE-INSTANCE is used to create an 
  222. object that is an instance of the class.  COMPILE-CLASS builds the 
  223. inheritance graph for a class by inspecting each of its mixins.  Until  a 
  224. class is "compiled" in this way, the procedure DESCRIBE is unable to describe
  225. completely all of its class variables, instance variables, and mixins.
  226.  
  227. This use of the term "compile" should not be confused with the compilation
  228. step of the Scheme evaluator, EVAL.  The term describes the process of
  229. collecting the pieces of the class definition into one place.  Compiling a
  230. class with COMPILE-CLASS does not prohibit further method definitions for the
  231. class, and adding method definitions to a class does not require that it be
  232. recompiled.
  233.  
  234. The order in which the classes in the inheritance graph are compiled with
  235. COMPILE-CLASS is immaterial.  Compiling a class does not automatically cause
  236. its mixins to be compiled, although the full graph containing them as nodes 
  237. is built in order to collect all the components of the compiled class.  Once 
  238. a mixin has been added to the inheritance graph in this way, the procedure
  239. DESCRIBE is able to describe all its components, although it may not have 
  240. been compiled itself.
  241.  
  242. Although a class is automatically compiled the first time MAKE-INSTANCE is
  243. used to create an instance of the class, it frequently is desirable to
  244. compile the class explicitly.  For example, when a source file containing 
  245. class definitions is to be compiled into object form with COMPILE-FILE, 
  246. explicit calls to COMPILE-CLASS cause the class to be compiled and the 
  247. inheritance structure to be constructed at the time the file is compiled.  
  248. Otherwise, the class is compiled every time the file is loaded and the first 
  249. MAKE-INSTANCE occurs.
  250.  
  251. Note that all the mixins of a class need to be defined before the class is
  252. compiled or its first instance  object is created.
  253.